// pages/orderlist/index.js
const pagesize = 10;
var utl = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabarflag:true,
    page:1,
    group:[],
    canpull: true,
    nomore:false,
    nodata:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryTrade();
  },
  //查询分润
  queryProfit:function(){
    var page = this.data.page;
    wx.showLoading({
      title: '请求中...',
    })
    utl.serverToken('/epay_apps/order/commissionListC0200', { 'pageSize': pagesize, 'pageNum': page }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var list = res.response.commissionList;
      var total_num = 0;
      var cp = this.data.canpull;
      var gp = this.data.group;

      for (var i = 0; i < list.length; i++) {
        total_num += list[i].list.length;
      }

      if (total_num == pagesize) {
        cp = true
      } else {
        cp = false;
      }
      if(gp.length==0){
        gp = gp.concat(list);
      }else{
        var last_gp = gp[gp.length - 1];
        var month = last_gp.month;
        var last_arr = last_gp.list;
        if (list.length > 0) {
          var list_gp = list[0];
          var lm = list_gp.month;
          if (lm == month) {
            var lm_arr = list_gp.list;
            list.splice(0, 1);
            last_gp.list = last_arr.concat(lm_arr);
            gp = gp.concat(list);
          } else {
            gp = gp.concat(list);
          }
        }
       
      }
      var gplen = gp.length;
      var station = true;
      if(gplen>0){
        station = false;
      }
      this.setData({
        group: gp,
        canpull: cp,
        nodata: gplen,
      })
    })
  },
  //查询所有交易
  queryTrade:function(){
    var page = this.data.page;
    wx.showLoading({
      title: '请求中...',
    })
    utl.serverToken('/epay_apps/order/tradeListC0100', { 'pageSize': pagesize, 'pageNum': page }, (err, res) => {
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
        return;
      }
      var list = res.response.tradeList;
      var total_num = 0;
      var cp = this.data.canpull;
      var gp = this.data.group;

      for(var i=0;i<list.length;i++){
        total_num += list[i].list.length;
      }
   
      if(total_num == pagesize){
        cp = true
      }else{
        cp = false;
       
      }
      if (gp.length == 0) {
        gp = gp.concat(list);
      } else {
        var last_gp = gp[gp.length - 1];
        var month = last_gp.month;
        var last_arr = last_gp.list;
        if (list.length > 0) {
          var list_gp = list[0];
          var lm = list_gp.month;
          if (lm == month) {
            var lm_arr = list_gp.list;
            list.splice(0, 1);
            last_gp.list = last_arr.concat(lm_arr);
            gp = gp.concat(list);
          } else {
            gp = gp.concat(list);
          }
        }
      }
      var gplen = gp.length;
      var station = true;
      if (gplen > 0) {
        station = false;
      }
      this.setData({
        group:gp,
        canpull:cp,
        nodata: gplen,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page;
    var sty = this.data.tabarflag;
    if (this.data.canpull){
      page++;
      this.setData({
        page:page
      })
      if(sty){
        this.queryTrade();
      }else{
        this.queryProfit();
      }
 
    }else{
      this.setData({
        nomore:true,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabbarAction: function () {
    this.setData({
      tabarflag: true,
      page: 1,
      group: [],
      canpull: true,
      nomore:false
    })
    this.queryTrade();
  },

  tabsection: function () {
    this.setData({
      tabarflag: false,
      page: 1,
      group: [],
      canpull: true,
      nomore:false,
    })
    this.queryProfit();
  }
})